package pt.spjm.pmartins.lapcounter;

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class Final_Results_Summary extends Activity {

	private Obj_CurrentData cData;
	private ListView lv;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.final_results_list);
		
		cData = LapCounter.cData;
		lv = (ListView) findViewById(R.id.lv_results);
		
		//c = this;
		
		SimpleAdapter sa = new SimpleAdapter(this, loadFinalResuts(), R.layout.final_results_list_row, 
				new String[] { "pos", "pNumber", "pName", "pLaps", "pAverage", "pFastest" },
				new int[] { R.id.fResults_pos, R.id.fResults_pNumber, R.id.fResults_pName, R.id.fResults_pLaps, R.id.fResults_pAverage, R.id.fResults_pFastest });
		lv.setAdapter(sa);
	}
	
	public ArrayList<HashMap<String, String>> loadFinalResuts(){
		ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		HashMap<String, String> row;

		int i = 0;
		for(Obj_Pilot p : cData.getPilots()){
			row = new HashMap<String, String>();
			row.put("pos", "" + ++i);
			row.put("pNumber", "" + p.getNumber());
			row.put("pName", p.getName());
			row.put("pLaps", "" + p.getNumberOfLaps());
			row.put("pAverage", getFormattedTime(p.getAverageTime()));
			row.put("pFastest", getFormattedTime(p.getFastestTime()));
			list.add(row);
		}
		return list;
	}

	
	public String getFormattedTime(Long time){
		GregorianCalendar gd = new GregorianCalendar();
		gd.setTimeInMillis( time );
		
		int min = gd.getTime().getMinutes();
		int s = gd.getTime().getSeconds();
		int ms = (int)((time % 1000) / 100);
		
		String sTime = "";
		if(min<10){
			sTime += "0" + min + ":";
		} else {
			sTime += min + ":";
		}
		
		if(s<10){
			sTime += "0" + s + ".";
		} else {
			sTime += s + ".";
		}
		sTime += ms;
		return sTime;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return true;
	}
}
